
screenCanvas = 0
gameScreen = 0
mapDis = 0
mouseFollower = 0
tilePalette = 0
app = 0
# Variable screen_name MUST be defined by receiving html.

class EditorApp
  constructor: () ->
    This = this
    @mode = "PlaceTile"
    @drawTime = 0
    @drawTimeLog = new Date().getTime()
    @nframes = 0
    screenCanvas.addEventListener('click',`function(e){This.onMouseClick(e)}`,false);
    addEventListener('keydown',`function(e){This.onKeyDown(e)}`,true)
    setInterval(`function(){This.redraw()}`,35)
    $("#button_openmap").click(`function(){This.openMap()}`)
    $("#button_savemap").click(`function(){This.saveMap()}`)
    $("#button_tileImages").click(()->This.mode="PlaceTile")
    $("#button_tileBlocks").click(()->This.mode="PlaceBlocks")
  onMouseClick: (e) ->
    mx = mouseFollower.gameX
    my = mouseFollower.gameY
    if 0<=mx<=mapDis.map.sizeX and 0<=my<=mapDis.map.sizeY
      if @mode is "PlaceTile"
          mapDis.map.layer0[mx][my] = new Tile(tilePalette.tileset,-tilePalette.offX,-tilePalette.offY)
      if @mode is "PlaceBlocks"
          mapDis.map.layer0[mx][my].blocks = not mapDis.map.layer0[mx][my].blocks
  onKeyDown: (e) ->
    if e.keyCode==37 #Left
      mapDis.pixeloffX-=20
      mapDis.pixeloffY-=10
    if e.keyCode==38 #up
      mapDis.pixeloffX+=20
      mapDis.pixeloffY-=10
    if e.keyCode==39 #right
      mapDis.pixeloffX+=20
      mapDis.pixeloffY+=10
    if e.keyCode==40 #down
      mapDis.pixeloffX-=20
      mapDis.pixeloffY+=10
  redraw: () ->
    t0 = new Date().getTime()
    mapDis.draw()
    if @mode is "PlaceBlocks"
      mapDis.drawBlocks()
    t1 = new Date().getTime()
    @drawTime+=t1-t0
    @nframes++
    if t1-@drawTimeLog>1000
      $("#DrawTime").html(Math.round(@drawTime/@nframes)+"ms ("+@nframes+")")
      @drawTimeLog = t1
      @drawTime = 0
      @nframes = 0
  saveMap: () ->
    console.log("___")
    if (confirm('Save Map "'+$("#current_map").html()+'"?'))
      $.post("../data/maps.php",{
          action:"save",
          username:"manux",
          password:"tintin",
          map:$("#current_map").html(),
          #data:lzw_encode(JSON.stringify(mapDis.map))},
          data:JSON.stringify(mapDis.map)},
        (data) ->
          $("#htmllog").html(data)
          status = data.substring(0,8)
          data = data.substring(8)
          console.log(status)
      )

  openMap: () ->
    r = prompt("Enter Map Name:","");
    if r!=null
      $.post("../data/maps.php",{
           action:"open",
           username:"manux",
           password:"tintin",
           map:r},
        (data) ->
          status = data.substring(0,8)
          data = data.substring(8)
          z =""
          map = JSON.parse(data)
          mapDis.load(map)
          $("#htmllog2").html(status)
          $("#current_map").html(r)
      )

exports.init = () ->
  screenCanvas = document.getElementById(screen_name)
  gameScreen = screenCanvas.getContext('2d')
  mapDis = new MapDisplayer(gameScreen)
  mouseFollower = new MouseFollower(screenCanvas)
  tilePalette = new TilePalette($("#tile_canvas")[0])
  app = new EditorApp()
  console.log("Done Init")
